package org.demo.base.mvc;

import lombok.Data;

/**
 * 所有Controller统一返回数据格式
 * @author tomdog
 */
@Data
public class HttpResult {

    /**
     * 状态码：200=成功，403=未登录，500=程序出错
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 返回的数据
     */
    private Object data;

    /**
     * 静态方法，构建响应结果
     */
    public static HttpResult success(String msg) {
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(200);// 最好编码写在统一的位置，用常量表示
        httpResult.setMsg(msg);
        return httpResult;
    }

    public static HttpResult success() {
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(200);// 最好编码写在统一的位置，用常量表示
        httpResult.setMsg("操作成功");
        return httpResult;
    }

    public static HttpResult success(Object data) {
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(200);// 最好编码写在统一的位置，用常量表示
        httpResult.setMsg("操作成功");
        httpResult.setData(data);
        return httpResult;
    }

    /**
     * 失败必须提供错误码和错误信息
     * @param msg
     * @return
     */
    public static HttpResult error(Integer code, String msg) {
        HttpResult httpResult = new HttpResult();
        httpResult.setCode(code);// 最好编码写在统一的位置，用常量表示
        httpResult.setMsg(msg);
        return httpResult;
    }
}
